﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Moonwish.Silverlight.OsAvatar.Windows
{
    /// <summary>
    /// 任务栏最小化任务项控件。
    /// write by zhlin 2012-01-10
    /// </summary>
    public partial class TaskBarItem : UserControl
    {
        private bool _isMouseInside;
        private Point _tempPoint;

        public event EventHandler Clicked;

        public TaskBarItem()
        {
            InitializeComponent();

            Icon = new BitmapImage(new Uri("/Moonwish.Silverlight.OsAvatar;component/Icons/app64.png", UriKind.Relative));

            MouseLeftButtonUp += UserControlMouseLeftButtonUp;
        }

        private void LayoutRootMouseEnter(object sender, MouseEventArgs e)
        {
            _isMouseInside = true;
           animEnter.Begin();
        }

        private void LayoutRootMouseLeave(object sender, MouseEventArgs e)
        {
            _isMouseInside = false;
            animLeave.Begin();
        }

        private void LayoutRootMouseMove(object sender, MouseEventArgs e)
        {
            _isMouseInside = true;
            Point position = e.GetPosition(this);
            _tempPoint.X = position.X / ActualWidth;
            _tempPoint.Y = 1.0;
            brushLight.Center = _tempPoint;
            brushLight.GradientOrigin = _tempPoint;
        }

        private void UserControlMouseLeftButtonUp(object sender, MouseEventArgs e)
        {
            if (Clicked != null)
            {
                Clicked(this, EventArgs.Empty);
            }
        }

        public void ApplyTheme()
        {
            //this.IsCaptionVisible = ((App)Application.Current).CurrentTheme.ShowTaskBarCaption;
            //base.Height = ((App)Application.Current).CurrentTheme.TaskBarItemSize.Height;
            //base.Width = ((App)Application.Current).CurrentTheme.TaskBarItemSize.Width;
            //this.imgIcon.Height = ((App)Application.Current).CurrentTheme.TaskBarIconSize.Height;
            //this.imgIcon.Width = ((App)Application.Current).CurrentTheme.TaskBarIconSize.Width;
            //this.brdInner.BorderBrush = ((App)Application.Current).CurrentTheme.InnerBorderBrush;
            //this.brdOuter.BorderBrush = ((App)Application.Current).CurrentTheme.OuterBorderBrush;
            //this.TransitionColor = ((App)Application.Current).CurrentTheme.TaskBarItemTransitionColor;
        }



        // Properties
        public string Caption
        {
            set { tbCaption.Text = value; }
        }

        public ImageSource Icon { set { imgIcon.Source = value; } get { return imgIcon.Source; } }

        private bool IsCaptionVisible
        {
            set
            {
                if (value)
                {
                    tbCaption.Visibility = Visibility.Collapsed;
                    ToolTipService.SetToolTip(LayoutRoot, null);
                }
                else
                {
                    tbCaption.Visibility = Visibility.Visible;
                    ToolTipService.SetToolTip(LayoutRoot, tbCaption.Text);
                }
            }
        }

        public Color TransitionColor
        {
            get { return transitionColor.Color; }
            set
            {
                transitionColor.Color = value;
                transitionSubColor.Color = Color.FromArgb(0x40, value.R, value.G, value.B);
            }
        }

    }
}
